package cn.kgc.waimai.controller;

import cn.kgc.waimai.common.CrowdFundingConstant;
import cn.kgc.waimai.pojo.User;
import cn.kgc.waimai.service.UserService;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;

/**
 * @author 陈啸
 * @create 2020-04-01 2:50
 */
@Controller
public class UserController {
    @Resource
    UserService userService;

    @RequestMapping("/index")
    public String index(){
        return "index-page";
    }

    //跳转登录
    @RequestMapping("/toLogin")
    public String toLogin(){
        return "index-login";
    }

    //登录
    @RequestMapping("/doLogin")
    public String login(String number, String password, Model model, HttpSession session){
        System.out.println(number);
        System.out.println(password);
        User user=userService.login(number,password);
        System.out.println(user);
        if(user==null){
            model.addAttribute(CrowdFundingConstant.ATTR_NAME_MESSAGE, CrowdFundingConstant.MESSAGE_LOGIN_FAILED);
            return "index-login";
        }else{
            session.setAttribute("user",user.getUsername());
            return "redirect:/getSelect";
        }
    }
}
